package cn.qiyu5522.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.qiyu5522.constant.SystemConsKeys;
import cn.qiyu5522.constant.SystemConstants;
import cn.qiyu5522.domain.VO.ArticleDetailVO;
import cn.qiyu5522.domain.VO.ArticleVO;
import cn.qiyu5522.domain.VO.HotArticleVo;
import cn.qiyu5522.domain.VO.PageVO;
import cn.qiyu5522.domain.entity.Article;
import cn.qiyu5522.domain.entity.Category;
import cn.qiyu5522.domain.entity.ResponseResult;
import cn.qiyu5522.mapper.ArticleMapper;
import cn.qiyu5522.service.ArticleService;
import cn.qiyu5522.service.CategoryService;
import cn.qiyu5522.utils.BeanCopyUtils;
import cn.qiyu5522.utils.RedisCache;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author Tonkey
 * @since 2024-04-09
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {


    @Resource
    private RedisCache redisCache;

    @Resource
    private CategoryService categoryService;

    @Override
    public ResponseResult hotArticleList() {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        // 查询条件 必须是正式文章
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        // 按照浏览量进行排序
        queryWrapper.orderByDesc(Article::getViewCount);
        // 最多只能查询10条消息
        Page<Article> page = new Page<>(1, 10);
        page(page, queryWrapper);
        List<Article> articles = page.getRecords();

        // VO 优化返回结果 涉及到 bean的拷贝
        // bean
        List<HotArticleVo> articleVos = BeanCopyUtils.copyBeanList(articles, HotArticleVo.class);
        // 封装结果集
        return ResponseResult.okResult(articleVos);
    }

    @Override
    public ResponseResult articleListWithPage(Integer pageNum, Integer pageSize, Long categoryId) {
        // 查询正常发布的文章
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL)
                // 根据 分类ID查询文章，无没有分类ID，则查全部
                .eq(ObjectUtil.isNotEmpty(categoryId) && categoryId > 0, Article::getCategoryId, categoryId)
                .orderByDesc(Article::getIsTop);
        // 分页条件
        Page<Article> page = new Page<>(pageNum, pageSize);
        baseMapper.selectPage(page, queryWrapper);
        // 获取文章列表
        List<Article> articles = page.getRecords();
        // 根据分类ID 去获取分类的名称
        articles = articles.stream()
                .peek(article -> article.setCategoryName(categoryService.getById(article.getCategoryId()).getName()))
                .collect(Collectors.toList());
        articles = articles.stream().map(article -> {
            Category category = categoryService.getById(article.getCategoryId());
            article.setCategoryName(category.getName());
            return article;
        }).collect(Collectors.toList());

        // 转 VO
        List<ArticleVO> articleVOS = BeanCopyUtils.copyBeanList(articles, ArticleVO.class);


        PageVO pageVO = new PageVO(articleVOS, page.getTotal());


        return ResponseResult.okResult(pageVO);
    }

    @Override
    public ResponseResult getArticleById(Long id) {
        // 条件：根据文章ID查询
        Article article = getById(id);

        // 从 redis 中获取viewCount
        Integer viewCount = redisCache.getCacheMapValue(SystemConsKeys.ARTICLE_VIEW_COUNT, id.toString());

        article.setViewCount(viewCount.longValue());

        // 转换成VO
        ArticleDetailVO articleDetailVO = BeanCopyUtils.copyBean(article, ArticleDetailVO.class);
        Category byId = categoryService.getById(article.getCategoryId());
        if (ObjectUtil.isNotEmpty(byId)){
            // 条件：根据分类ID查询分类名称,并想分类名称赋值给 articleDetailVO类
            articleDetailVO.setCategoryName(byId.getName());
        }
        return ResponseResult.okResult(articleDetailVO);
    }

    @Override
    public ResponseResult updateViewCount(Long id) {
        // 更新redis中对应 id的浏览量
        redisCache.incrementCacheMapValue(SystemConsKeys.ARTICLE_VIEW_COUNT,id.toString(),SystemConstants.CHANGE_ARTICLE_VIEW_COUNT);
        return ResponseResult.okResult();
    }



}
